package com.yunkazaixian.syntelecom.worker;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunkazaixian.syntelecom.entity.McSimDx;
import com.yunkazaixian.syntelecom.mapper.McSimDxMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.concurrent.BlockingQueue;

public class RequestWorker implements Runnable {
    private final BlockingQueue<McSimDx> requestQueue;
    private final McSimDxMapper simDxMapper;

    // 通过构造函数注入 McSimDxMapper
    @Autowired
    public RequestWorker(BlockingQueue<McSimDx> requestQueue, McSimDxMapper simDxMapper) {
        this.requestQueue = requestQueue;
        this.simDxMapper = simDxMapper;
    }

    @Override
    public void run() {
        try {
            while (true) {
                McSimDx task = requestQueue.take();
                processTask(task);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private void processTask(McSimDx mcSimDx) {
        // System.out.println("Processing request with ID: " + task.getRequestId() + ", Data: " + task.getRequestData());

        // 模拟处理耗时
        // Thread.sleep(1000);
        LambdaUpdateWrapper<McSimDx> lambdaUpdateWrapper = new LambdaUpdateWrapper<McSimDx>()
    .eq(McSimDx::getIccid, mcSimDx.getIccid());
        if(mcSimDx.getNetworkOutage() != null){
            lambdaUpdateWrapper.set(McSimDx::getNetworkOutage, mcSimDx.getNetworkOutage());
        }
        if(StringUtils.isNotBlank(mcSimDx.getCardboard())){
            lambdaUpdateWrapper.set(McSimDx::getCardboard, mcSimDx.getCardboard());
        }
        if(StringUtils.isNotBlank(mcSimDx.getActiveTime())){
            lambdaUpdateWrapper.set(McSimDx::getActiveTime, mcSimDx.getActiveTime());
        }
        if(StringUtils.isNotBlank(mcSimDx.getArriveTime())){
            lambdaUpdateWrapper.set(McSimDx::getArriveTime, mcSimDx.getArriveTime());
        }
        if(StringUtils.isNotBlank(mcSimDx.getIsOutThreshold())){
            lambdaUpdateWrapper.set(McSimDx::getIsOutThreshold, mcSimDx.getIsOutThreshold());
        }
        if(StringUtils.isNotBlank(mcSimDx.getImPayStatus())){
            lambdaUpdateWrapper.set(McSimDx::getImPayStatus, mcSimDx.getImPayStatus());
        }
        // 上线时间
        if(StringUtils.isNotBlank(mcSimDx.getImSaleProvince())){
            lambdaUpdateWrapper.set(McSimDx::getImSaleProvince, mcSimDx.getImSaleProvince());
            lambdaUpdateWrapper.set(McSimDx::getImSaleCity, null);
        }
        // 下线时间
        if(StringUtils.isNotBlank(mcSimDx.getImSaleCity())){
            lambdaUpdateWrapper.set(McSimDx::getImSaleCity, mcSimDx.getImSaleCity());
        }
        lambdaUpdateWrapper.set(McSimDx::getUpdateTime, LocalDateTime.now());
        simDxMapper.update(lambdaUpdateWrapper);
    }
}
